#!/bin/sh -ex

DIR="$1"

case $(uname -s) in
  Darwin)
    brew install curl zlib pcre2 openssl
    brew reinstall curl zlib pcre2 openssl
    for i in curl zlib pcre2 openssl
    do
      brew unlink $i || true;
      brew link --force $i || true
    done;;
  Linux)
    export DEBIAN_FRONTEND=noninteractive
    sed -e 's/^deb/deb-src/' /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/src.list
    sudo apt-get update
    sudo apt-get install build-essential
    sudo apt-get -y build-dep git;;
esac

cd "$DIR"
printf "%s\n" \
  "NO_GETTEXT=YesPlease" \
  "NO_OPENSSL=YesPlease" \
  "prefix=/usr/local" \
  > config.mak
make -j2
sudo make install

echo "Git version:"
git --version
